<?

/**
 * Time constant - the number of seconds in a year
 */

define('YEARSECS', 31536000);

/**
 * Time constant - the number of seconds in a week
 */
define('WEEKSECS', 604800);

/**
 * Time constant - the number of seconds in a day
 */
define('DAYSECS', 86400);

/**
 * Time constant - the number of seconds in an hour
 */
define('HOURSECS', 3600);

/**
 * Time constant - the number of seconds in a minute
 */
define('MINSECS', 60);

/**
 * Time constant - the number of minutes in a day
 */
define('DAYMINS', 1440);

/**
 * Time constant - the number of minutes in an hour
 */
define('HOURMINS', 60);

function print_error($text){
 global $reportdir;

 $reporthtml = str_replace('{error}', $text, file_get_contents($dir.'/report.tpl.html'));
 file_put_contents($reportdir.'/index.html', $reporthtml);
 
 die;
}

function stacked_graph($data,$labels,$imgpath, $title){
 global $dir;
 // Standard inclusions     
 include_once($dir."/pChart/pData.class");  
 include_once($dir."/pChart/pChart.class");  

  
 // Dataset definition   
 $DataSet = new pData;  

 foreach($data as $key=>$val){
    $DataSet->AddPoint($val,"Serie".$key);  
 }   
 
 $DataSet->AddAllSeries();  
 $DataSet->SetAbsciseLabelSerie();  
 
 foreach($labels as $key=>$val){
    $DataSet->SetSerieName($val,"Serie".$key);  
 }    
  
 // Initialise the graph  
 $Test = new pChart(900,230);  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",8);  
 $Test->setGraphArea(50,30,680,200);  
 $Test->drawFilledRoundedRectangle(7,7,893,223,5,240,240,240);  
 $Test->drawRoundedRectangle(5,5,895,225,5,230,230,230);  
 $Test->drawGraphArea(255,255,255,TRUE);  
 $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);  
 $Test->drawGrid(4,TRUE,230,230,230,50);  
  
 // Draw the 0 line  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",6);  
 $Test->drawTreshold(0,143,55,72,TRUE,TRUE);  
  
 // Draw the bar graph  
 $Test->drawStackedBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);  
  
 // Finish the graph  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",8);  
 $Test->drawLegend(696,10,$DataSet->GetDataDescription(),255,255,255);  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",10);  
 $Test->drawTitle(50,22,$title,50,50,50,585);
 
 $Test->Render($imgpath);
}



function radar_graph($data,$labels,$imgpath){
 global $dir;
 // Standard inclusions     
 include_once($dir."/pChart/pData.class");  
 include_once($dir."/pChart/pChart.class");  

 $radar = array();
 for($i = 0; $i<count($labels); $i++){
	$radar[$labels[$i]] = $data[$i];
 }
 ksort($radar);
 $labels = array();
 $data = array();
 foreach($radar as $r=>$v){
  $labels[] = $r;
  $data[] = $v;
 }
  
 // Dataset definition   
 $DataSet = new pData;  
 $DataSet->AddPoint($labels,"Label");  
 $DataSet->AddPoint($data,"Serie1");  
   
 $DataSet->AddSerie("Serie1");    
 $DataSet->SetAbsciseLabelSerie("Label");    
  
 $DataSet->SetSerieName("Reference","Serie1");  
  
 // Initialise the graph  
 $Test = new pChart(400,400);  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",8);  
 $Test->drawFilledRoundedRectangle(7,7,393,393,5,240,240,240);  
 $Test->drawRoundedRectangle(5,5,395,395,5,230,230,230);  
 $Test->setGraphArea(30,30,370,370);  
 $Test->drawFilledRoundedRectangle(30,30,370,370,5,255,255,255);  
 $Test->drawRoundedRectangle(30,30,370,370,5,220,220,220);  
  
 // Draw the radar graph  
 $Test->drawRadarAxis($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE,20,120,120,120,230,230,230);  
 $Test->drawFilledRadar($DataSet->GetData(),$DataSet->GetDataDescription(),50,20);  
  
 // Finish the graph  
 $Test->drawLegend(15,15,$DataSet->GetDataDescription(),255,255,255);  
 $Test->setFontProperties($dir."Fonts/tahoma.ttf",10);  
 $Test->Render($imgpath);
}
 
function pie_graph($data,$labels,$imgpath){
 global $dir;
 
 include_once($dir."/pChart/pData.class");  
 include_once($dir."/pChart/pChart.class");  
  
 // Dataset definition   
 $DataSet = new pData;  
 $DataSet->AddPoint($data,"Serie1");  
 $DataSet->AddPoint($labels,"Serie2");  
 $DataSet->AddAllSeries();  
 $DataSet->SetAbsciseLabelSerie("Serie2");  
  
 // Initialise the graph  
 $Test = new pChart(480,200);  
 $Test->drawFilledRoundedRectangle(7,7,473,193,5,240,240,240);  
 $Test->drawRoundedRectangle(5,5,475,195,5,230,230,230);  
  
 // Draw the pie chart  
 $Test->setFontProperties($dir."/Fonts/tahoma.ttf",8);  
 $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
 $Test->drawPieLegend(300,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);  
  
 $Test->Render($imgpath);
}

function right_to_left() {
  static $result;

  if (isset($result)) {
	  return $result;
  }
  return $result = ('ltr' == 'rtl');
}

 function fix_align_rtl($align) {
     if (!right_to_left()) {
         return $align;
     }
      if ($align=='left')  { return 'right'; }
      if ($align=='right') { return 'left'; }
      return $align;
}

function print_table($table, $return=false) {
    $output = '';

    if (isset($table->align)) {
        foreach ($table->align as $key => $aa) {
            if ($aa) {
                $align[$key] = ' text-align:'. fix_align_rtl($aa) .';';  // Fix for RTL languages
            } else {
                $align[$key] = '';
            }
        }
    }
    if (isset($table->size)) {
        foreach ($table->size as $key => $ss) {
            if ($ss) {
                $size[$key] = ' width:'. $ss .';';
            } else {
                $size[$key] = '';
            }
        }
    }
    if (isset($table->wrap)) {
        foreach ($table->wrap as $key => $ww) {
            if ($ww) {
                $wrap[$key] = ' white-space:nowrap;';
            } else {
                $wrap[$key] = '';
            }
        }
    }

    if (empty($table->width)) {
        $table->width = '80%';
    }

    if (empty($table->tablealign)) {
        $table->tablealign = 'center';
    }

    if (!isset($table->cellpadding)) {
        $table->cellpadding = '5';
    }

    if (!isset($table->cellspacing)) {
        $table->cellspacing = '1';
    }

    if (empty($table->class)) {
        $table->class = 'generaltable';
    }

    $tableid = empty($table->id) ? '' : 'id="'.$table->id.'"';

    $output .= '<table width="'.$table->width.'" ';
    if (!empty($table->summary)) {
        $output .= " summary=\"$table->summary\"";
    }
    $output .= " cellpadding=\"$table->cellpadding\" cellspacing=\"$table->cellspacing\" class=\"$table->class boxalign$table->tablealign\" $tableid>\n";

    $countcols = 0;
    
    if (!empty($table->head)) {
        $countcols = count($table->head);
        $output .= '<tr>';
        $keys=array_keys($table->head);
        $lastkey = end($keys);
        foreach ($table->head as $key => $heading) {

            if (!isset($size[$key])) {
                $size[$key] = '';
            }
            if (!isset($align[$key])) {
                $align[$key] = '';
            }
            if ($key == $lastkey) {
                $extraclass = ' lastcol';
            } else {
                $extraclass = '';
            }

            $output .= '<th style="vertical-align:top;'. $align[$key].$size[$key] .';white-space:nowrap;" class="header c'.$key.$extraclass.'" scope="col">'. $heading .'</th>';
        }
        $output .= '</tr>'."\n";
    }

    if (!empty($table->data)) {
        $oddeven = 1;
        $keys=array_keys($table->data);
        $lastrowkey = end($keys);
        foreach ($table->data as $key => $row) {
            $oddeven = $oddeven ? 0 : 1;
            if (!isset($table->rowclass[$key])) {
                $table->rowclass[$key] = '';
            }
            if ($key == $lastrowkey) {
                $table->rowclass[$key] .= ' lastrow';
            }
            $output .= '<tr class="r'.$oddeven.' '.$table->rowclass[$key].'">'."\n";
            if ($row == 'hr' and $countcols) {
                $output .= '<td colspan="'. $countcols .'"><div class="tabledivider"></div></td>';
            } else {  /// it's a normal row of data
                $keys2=array_keys($row);
                $lastkey = end($keys2);
                foreach ($row as $key => $item) {
                    if (!isset($size[$key])) {
                        $size[$key] = '';
                    }
                    if (!isset($align[$key])) {
                        $align[$key] = '';
                    }
                    if (!isset($wrap[$key])) {
                        $wrap[$key] = '';
                    }
                    if ($key == $lastkey) {
                      $extraclass = ' lastcol';
                    } else {
                      $extraclass = '';
                    }
                    $output .= '<td style="'. $align[$key].$size[$key].$wrap[$key] .'" class="cell c'.$key.$extraclass.'">'. $item .'</td>';
                }
            }
            $output .= '</tr>'."\n";
        }
    }
    $output .= '</table>'."\n";

    if ($return) {
        return $output;
    }

    echo $output;
    return true;
}

 function format_time($totalsecs, $str=NULL) {

    $totalsecs = abs($totalsecs);

    if (!$str) {  // Create the str structure the slow way
        $str->hour  = 'hour';
        $str->hours = 'hours';
        $str->min   = 'min';
        $str->mins  = 'mins';
        $str->sec   = 'sec';
        $str->secs  = 'secs';      
    }

    
    $hours     = floor($totalsecs/HOURSECS);
    $remainder = $totalsecs - ($hours*HOURSECS);
    $mins      = floor($remainder/MINSECS);
    $secs      = $remainder - ($mins*MINSECS);

    $ss = ($secs == 1)  ? $str->sec  : $str->secs;
    $sm = ($mins == 1)  ? $str->min  : $str->mins;
    $sh = ($hours == 1) ? $str->hour : $str->hours;


    $ohours = '';
    $omins = '';
    $osecs = '';

    if ($hours) $ohours = $hours .' '. $sh;
    if ($mins)  $omins  = $mins .' '. $sm;
    if ($secs)  $osecs  = $secs .' '. $ss;

    if ($hours) return trim($ohours .' '. $omins);
    if ($mins)  return trim($omins .' '. $osecs);
    if ($secs)  return $osecs;
    return '';
}


?>